﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Biz_Dal.Biz;

public partial class The_hien_TH_DANG_NHAP : System.Web.UI.UserControl
{
    BizKhachHang khachhang;
    void DaDN()
    {
        PanelDaDN.Visible = true;
        PanelChuaDN.Visible = false;
        LabelHoTen.Text = Session["Ho_ten"] as string;
        LabelMaKH.Text = Session["Ma_KH"] as string;
    }

    void ChuaDN()
    {
        LabelHoTen.Text = "";
        LabelMaKH.Text = "";
        PanelDaDN.Visible = false;
        PanelChuaDN.Visible = true;
    }

    void QuanTri()
    {
        if (Session["Dang_nhap"] == "1")
            Panel1.Visible = true;
        else
            Panel1.Visible = false;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Dang_nhap"] == null)
        {
            ChuaDN();
        }
        else
        {
            DaDN();
            LabelMaKH.Text = Session["Ma_KH"] as string;
            LabelHoTen.Text = Session["Ho_ten"] as string;
            QuanTri();
        }
    }
    protected void btnDN_Click(object sender, EventArgs e)
    {
        List<BizKhachHang> list = BizKhachHang.Login(txtTenDN.Text, txtMK.Text);
        if (list.Count == 0)
        {
            Response.Write("<script>alert('Đăng nhập sai');</script>");
        }
        else
        {
            foreach (BizKhachHang lgKH in list)
            {
                if (!lgKH.laUserXau)
                {
                    Session["Ma_KH"] = (lgKH.idUser).ToString();
                    Session["Ho_ten"] = lgKH.hoTen;
                    Session["Khach_hang"] = lgKH;
                    if (lgKH.laQuanTri)
                        Session["Dang_nhap"] = "1";
                    else
                        Session["Dang_nhap"] = "0";
                    Response.Write("<script>alert('Đăng nhập thành công');</script>");
                    DaDN();
                    Response.Redirect("~/Trang_web/Trang_chu.aspx");
                }
                else
                    Response.Write("<script>alert('Tài khoản bị khoá. Vui lòng liên hệ admin');</script>");
                
            }
        }

    }
    protected void btnDangXuat_Click(object sender, EventArgs e)
    {
       
        Session["Dang_nhap"] = null;
        Session["Ma_KH"] = null;
        Session["Ho_ten"] = null;
        Session["Khach_hang"] = null;
        ChuaDN();
        Response.Redirect("~/Trang_web/Trang_chu.aspx");
    }
}